package com.Tools.Calligraphy
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class CenterWrite extends Sprite
	{
		[Embed(source="com/Tools/Calligraphy/Brush/center_long_1.png")]
		private var c1:Class;
		
		[Embed(source="com/Tools/Calligraphy/Brush/center_long_2.png")]
		private var c2:Class;
		
		[Embed(source="com/Tools/Calligraphy/Brush/center_short_1.png")]
		private var c3:Class;
		
		[Embed(source="com/Tools/Calligraphy/Brush/center_short_2.png")]
		private var c4:Class;
		
		//旋转角度弧度计算
		private var gdi:Number=180/3.14175;
		
		public function CenterWrite(_begin:Point,_end:Point)
		{
			super();
			
			var local:Point=Point.interpolate(_begin,_end,0.5);
			
			x=local.x;
			y=local.y;
			
			//生成长度  选择笔墨
			var long:Number=Point.distance(_begin,_end);
			var temp:DisplayObject;
			
			if(long<=40){
				temp=new c4();
			}else if(long>40 && long<=70){
				temp=new c3();
			}else{
				temp=new c2();
				temp.width=long+20;
			}
			
			temp.x=-temp.width/2;
			temp.y=-temp.height/2;
			
			this.rotation=getAngleTrig(_end.x-_begin.x,_end.y-_begin.y);
			
			
			this.addChild(temp);
			
			
			
			
		}
		
		//计算角度值
		private function getAngleTrig(X:Number, Y:Number): Number
		{
			if (X == 0)
			{
				if(Y < 0){
					return 270;
				}else{
					return 90;
				}
			} else if (Y == 0)
			{
				if(X < 0){
					return 180;
				}else{
					return 0;
				}
			}
			
			if ( Y > 0)
			{
				if (X > 0)
				{
					return Math.atan(Y/X) * gdi;
				}else
				{
					return 180-Math.atan(Y/-X) * gdi;
				}
			}else
			{
				if (X > 0.0)
				{
					return 360-Math.atan(-Y/X) * gdi;
				}else
				{
					return 180+Math.atan(-Y/-X) *gdi;
				}
			}
		} 	
	}
}